<template>
  <div class="base-table">
    <el-table v-bind="$attrs" :data="tableData" :border="border" :stripe="stripe" v-loading="loading"
      style="width: 100%">
      <slot />

      <!-- 空数据插槽 -->
      <template #empty>
        <el-empty :description="emptyText" />
      </template>
    </el-table>

    <!-- 分页 -->
    <div v-if="showPagination" class="pagination-container">
      <el-pagination background v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="pageSizes"
        :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
  // 表格数据
  data: {
    type: Array,
    default: () => []
  },
  // 是否显示边框
  border: {
    type: Boolean,
    default: true
  },
  // 是否显示斑马纹
  stripe: {
    type: Boolean,
    default: false
  },
  // 加载状态
  loading: {
    type: Boolean,
    default: false
  },
  // 空数据文本
  emptyText: {
    type: String,
    default: '暂无数据'
  },
  // 是否显示分页
  showPagination: {
    type: Boolean,
    default: true
  },
  // 总条数
  total: {
    type: Number,
    default: 0
  },
  // 当前页码
  page: {
    type: Number,
    default: 1
  },
  // 每页条数
  limit: {
    type: Number,
    default: 10
  },
  // 每页条数选项
  pageSizes: {
    type: Array,
    default: () => [10, 20, 30, 50]
  }
})

const emit = defineEmits(['update:page', 'update:limit', 'pagination'])

const currentPage = computed({
  get: () => props.page,
  set: (val) => emit('update:page', val)
})

const pageSize = computed({
  get: () => props.limit,
  set: (val) => emit('update:limit', val)
})

const tableData = computed(() => props.data)

/**
 * @description 处理每页条数变化
 */
const handleSizeChange = (val) => {
  emit('pagination', { page: currentPage.value, limit: val })
}

/**
 * @description 处理页码变化
 */
const handleCurrentChange = (val) => {
  emit('pagination', { page: val, limit: pageSize.value })
}
</script>

<style lang="scss" scoped>
.base-table {
  .pagination-container {
    padding: 20px 0;
    display: flex;
    // justify-content: center;
    justify-content: flex-end;
  }
}
</style>